package xhwl.hmo.mall.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import xhwl.hmo.mall.IntentConstants;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.ui.InfoOrderActivity;
import xhwl.hmo.mall.utils.XHLog;


/**
 * Created by admin on 2017-11-10.
 */

public class AlarmReceiver extends BroadcastReceiver {
    private static final String TAG = "AlarmReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        String action = intent.getAction();
        XHLog.e("alarm receive="+action);
        if (action.equals(IntentConstants.Action.orderRemind)) {

            String orderId = intent.getStringExtra(IntentConstants.EXTRA_REMIND_ORDER_ID);
//            String goodsType = intent.getStringExtra(IntentConstants.EXTRA_GOODS_TYPE);
//            showNotificationRemindMe(context, matchID);
//            ToastUtil.showToast(context,orderId);
            remind(context,orderId);

        }

    }
    private void remind(Context context,String orderId){
        Intent intent = new Intent(context,InfoOrderActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(InfoOrderActivity.EXTRA_ORDER_ID,orderId);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context, Integer.parseInt(orderId), intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder builder= new Notification.Builder(context);
        builder.setSmallIcon(R.mipmap.icon);
        builder.setContentTitle("提示");
        builder.setContentText("您的订单即将过期，请尽快付款");
        builder.setAutoCancel(true);
        builder.setDefaults(Notification.DEFAULT_ALL);//铃声,振动,呼吸灯
        builder.setContentIntent(pendingIntent);

        Notification noti =builder.build();

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService((context.NOTIFICATION_SERVICE));
        mNotificationManager.notify(Integer.parseInt(orderId), noti);
    }
}